//
//  BPWiFiView.m
//  BPTV
//
//  Created by baipeng on 2018/11/16.
//  Copyright © 2018年 Apple Inc. All rights reserved.
//

#import "BPWiFiView.h"
@interface BPWiFiView ()
@property (nonatomic, weak) UIImageView *imageView;
@property (nonatomic, assign) CGSize imageSize;
@property (nonatomic, weak) UILabel *descLabel;
@property (nonatomic, weak) UIScrollView *scrollView;

@end

@implementation BPWiFiView
#pragma mark - life cyle 1、控制器生命周期
-(void)layoutSubviews{
    [super layoutSubviews];
    if (!self.imageSize.width || !self.imageSize.height || !self.descLabel.font) {
        return;
    }
    CGSize contentSize = self.frame.size;
    self.scrollView.frame = self.bounds;
    CGFloat imageW = contentSize.width * 0.8f;
    CGFloat display2RealFactor = imageW / self.imageSize.width;
    CGFloat imageH = self.imageSize.height * display2RealFactor;
    CGRect imageFrame = self.imageView.frame;
    imageFrame.size = CGSizeMake(imageW, imageH);
    self.imageView.frame = imageFrame;
    self.imageView.center = CGPointMake(contentSize.width * 0.5f, imageH * 0.5f + 60);
    CGSize labelSize = [self.descLabel.text boundingRectWithSize:CGSizeMake(contentSize.width - 30, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.descLabel.font} context:nil].size;
    CGFloat labelW = labelSize.width;
    CGFloat labelH = labelSize.height;
    CGFloat labelX = (contentSize.width - labelW) * 0.5f;
    CGFloat labelY = CGRectGetMaxY(self.imageView.frame) + 20;
    self.descLabel.frame = CGRectMake(labelX, labelY, labelW, labelH);
    self.scrollView.contentSize = CGSizeMake(0, CGRectGetMaxY(self.descLabel.frame) + 20);

}
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self commmonInit];
    }
    return self;
}

#pragma mark - 2、不同业务处理之间的方法以
-(void)setAddress:(NSString *)address{
    self.descLabel.text = address = [self descTextWithAdress:address];
}

#pragma mark - Network 3、网络请求

#pragma mark - Action Event 4、响应事件

#pragma mark - Call back 5、回调事件
-(void)commmonInit{
    self.backgroundColor = kRGBColor(244, 244, 244);
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    self.scrollView = scrollView;
    [self addSubview:scrollView];
    UIImage *image = [UIImage imageNamed:@"BPWiFiUpload.bundle/wifi.png"];
    self.imageSize = image.size;
    UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
    self.imageView = imageView;
    [self.scrollView addSubview:imageView];
    UILabel *descLabel = [[UILabel alloc]init];
    descLabel.font = [UIFont systemFontOfSize:15];
    descLabel.numberOfLines = 0;
    descLabel.text = [self descTextWithAdress:@"Fetching..."];
    self.descLabel = descLabel;
    [self.scrollView addSubview:descLabel];

}

//TODO:描述
-(NSString *)descTextWithAdress:(NSString *)address{
    return [NSString stringWithFormat:@"1. Please make sure your iPhone and PC is connected to the same WiFi.\n\n2. Open the URL below in a browser:\n\n%@\n\n3. When click the submit button on the website, please wait until the page finish loading.",address];
}

#pragma mark - Delegate 6、代理、数据源

#pragma mark - interface 7、UI处理

#pragma mark - lazy loading 8、懒加载


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
